package nc.impl.ftm.customsinvoice.customsinvoice;

import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.ui.pub.print.IMetaDataDataSource;
import nc.vo.ftm.customsinvoice.AggCustomsInvoiceVO;

import java.math.BigDecimal;
import nc.vo.ftm.customsinvoice.CustomsInvoiceVO;
import nc.vo.ftm.customsinvoice.CustomsinvoiceBVO;

public class AggCustomsInvoiceVOPrintDataSource implements IMetaDataDataSource {

	private static final long serialVersionUID = 1L;

	private String[] oids;

	public AggCustomsInvoiceVOPrintDataSource(String[] oids) {
		this.oids = oids;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object[] getMDObjects() {
		//		IMDPersistenceQueryService bs = NCLocator.getInstance().lookup(IMDPersistenceQueryService.class);
		//		AggCustomsInvoiceVO[] aggvos = new AggCustomsInvoiceVO[] {};
		//		try {
		//			aggvos = (AggCustomsInvoiceVO[]) bs.queryBillOfVOByPKs(AggCustomsInvoiceVO.class, this.oids, false).toArray(new AggCustomsInvoiceVO[0]);
		//		} catch (MetaDataException e) {
		//			ExceptionUtils.wrapException(e);
		//		}
		BillQuery<AggCustomsInvoiceVO> billQuery = new BillQuery<>(AggCustomsInvoiceVO.class);
		AggCustomsInvoiceVO[] aggvos = billQuery.query(this.oids);
		

		if(aggvos != null && aggvos.length != 0) {
			for(AggCustomsInvoiceVO aggvo : aggvos) {
				CustomsinvoiceBVO[] aggbvos = (CustomsinvoiceBVO[])aggvo.getAllChildrenVO();
				CustomsInvoiceVO agghvo =  aggvo.getParentVO();
				
				String mdg = "";
				String mz = "";
				
				
				if(null != agghvo.getDestination_port()) {
					mdg = agghvo.getDestination_port();
				}
				
				if(null != agghvo.getDef3()) {
					mz = agghvo.getDef3();
				}
				
				String  js = "0";
				String tj = "0";
				for(CustomsinvoiceBVO aggbvo : aggbvos) {

					aggbvo.setDef13(mdg);
					aggbvo.setDef14(mz);
					
					if(null != aggbvo.getDef3() && !"~".equals(aggbvo.getDef3())) {
						js = new BigDecimal(js).add(new BigDecimal(aggbvo.getDef3())).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
					}
					if(null != aggbvo.getDef4() && !"~".equals(aggbvo.getDef4())) {
						tj = new BigDecimal(tj).add(new BigDecimal(aggbvo.getDef4())).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
//						tj = MathTool.add(new BigDecimal(tj), new BigDecimal(aggbvo.getDef4())).toString();
					}
				}
				agghvo.setDef27(js);
				agghvo.setDef28(tj);
			}
		}
		return aggvos;
		
	}

	@Override
	public String[] getItemValuesByExpress(String itemExpress) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isNumber(String itemExpress) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String[] getDependentItemExpressByExpress(String itemExpress) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] getAllDataItemExpress() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] getAllDataItemNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getModuleName() {
		// TODO Auto-generated method stub
		return null;
	}

}
